package com.dhecp.project.logstics.controller;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.business.domain.LogisticsMmodeGrads;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.logstics.mapper.LogsticsMmodeGradsMapper;
import com.dhecp.project.logstics.service.LogisticsMmodeGradsService;
import com.dhecp.project.system.domain.SysDictionaryForPageField;
import com.dhecp.project.system.service.SysDictionaryForFieldService;


/**
 * 物流方式梯度
 * @Author fxz
 * @Date 2023/03/23
 */
@Controller
@RequestMapping("/logstics/logisticsmmodegrads")
public class LogsticsMmodeGradsController extends BaseModuleController {

    private String prefix = "logstics/logisticsmmodeGrads";
    private String moduleTable = ModuleTable.LOGISTICS_MMODE_GRADS;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    @Autowired
    private LogsticsMmodeGradsMapper logsticsMmodeGradsMapper;

    @Autowired
    private LogisticsMmodeGradsService logisticsMmodeGradsService;

    /**
     * 打开基础数据列表
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:view")
    @GetMapping()
    public ModelAndView baseinfo(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
        params.put("entity", new LogisticsMmodeGrads()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段

        Subject userSubject = ShiroUtils.getSubject();
        Integer toolBarWidth = 0;
        if (userSubject.isPermitted("logstics:logisticsmmodegrads:delete")){	//当前登录用户主体是否有删除用户的权限
//        	params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
            toolBarWidth = toolBarWidth + 100;
        }
        if (userSubject.isPermitted("logstics:logisticsmmodegrads:edit")){ //当前登录用户主体是否有修改的权限
            toolBarWidth = toolBarWidth + 100;
        }
        params.put("toolBarWidth", toolBarWidth);

        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "moduleTable", "功能唯一标识");
        }
        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "type","基础数据分项");
        }
        if (mv.isEmpty()){
//            if ("TP_LogisticsMmodeGrads".toLowerCase().equals(params.get("moduleTable").toString().toLowerCase())){
//                String moduleTable = params.get("moduleTable").toString() + "_" + params.get("vtype");
//                moduleTable = (moduleTable.toLowerCase().indexOf("tb_baseinfo_area") == -1 ? moduleTable : "tb_baseinfo_area");
//                params.put("moduleTable", moduleTable);
//            }
            Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
            AjaxResult ajaxResult = new AjaxResult();
            try{
                dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
                if (dictionaryForFieldMap.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                    ajaxResult = AjaxResult.success("数据获取成功。");
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("moduleTable", params.get("moduleTable").toString());
            mv.addObject("type", params.get("type"));
            mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
            mv.addObject("ajaxResult", ajaxResult);
            mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
            mv.setViewName(prefix + "/logisticsmmodeGrads");
        }
        return mv;
    }

    /**
     *@Description: 查询页 列表数据
     * @Author: fxz
     * @Date 2023/03/23
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        List<LogisticsMmodeGrads> list = new ArrayList<LogisticsMmodeGrads>();
        try {
            list = logisticsMmodeGradsService.queryList(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
     * 打开基础数据新增页
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:add")
    @GetMapping("/add")
    public ModelAndView baseInfoAdd(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();
        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "moduleTable","基础数据表标识");
        }
        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "type","基础数据分项");
        }
        if (mv.isEmpty()){
            params.put("entity", new LogisticsMmodeGrads());
            AjaxResult ajaxResult = new AjaxResult();
            List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>(); //字段字典
            List<Map<String, Object>> fieldsListTemp = new ArrayList<Map<String, Object>>(); //临时存储区，用于存放需要放到最后生成的控件，如：备注等大文本控件
            List<Object> fieldsList = new ArrayList<Object>(); 			//生成控件的字段和属性
            List<Object> fieldsHiddenList = new ArrayList<Object>(); 	//隐藏的字段和属性
            try{
                params.put("getFieldAttributesType", "pagenotingrid"); 	//非表格字段
                dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
                if (dicFieldsList.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                    ajaxResult = AjaxResult.success("数据获取成功。");
                    LogisticsMmodeGrads logisticsMmodeGrads = new LogisticsMmodeGrads();
                    logisticsMmodeGrads.setType(params.get("type").toString());
                    for (SysDictionaryForPageField dicFields : dicFieldsList){
                        Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);
                        Map<String, Object> baseInfoMap = TypeUtils.entityToMap(logisticsMmodeGrads);
                        fieldsListMap.put("fieldContent", baseInfoMap.get(fieldsListMap.get("fields")));

                        if (fieldsListMap.get("fieldFrontType") == null){
                            fieldsListMap.put("fieldFrontType", "t_text");
                        }

                        if ("t_textarea".equals(fieldsListMap.get("fieldFrontType").toString())){ //大文本控件放入临时存储区
                            fieldsListTemp.add(fieldsListMap);
                        }else if ((Integer)fieldsListMap.get("fieldFrontTop") == 1){	//显示的字段
                            fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
                        }else if ((Integer)fieldsListMap.get("fieldFrontTop") == 2){	//隐藏的字段
                            fieldsHiddenList.add(TypeUtils.mapToJson(fieldsListMap));
                        }
                    }
                    if (!fieldsListTemp.isEmpty() && fieldsListTemp.size() > 0){
                        for (Map<String, Object> fieldsMap : fieldsListTemp){ 			//大文本控件追加到数组末尾
                            if ((Integer)fieldsMap.get("fieldFrontTop") == 1){			//显示的字段
                                fieldsList.add(TypeUtils.mapToJson(fieldsMap));
                            }else if ((Integer)fieldsMap.get("fieldFrontTop") == 2){	//隐藏的字段
                                fieldsHiddenList.add(TypeUtils.mapToJson(fieldsMap));
                            }
                        }
                    }
                }
                mv.addObject("fieldsList", fieldsList);				//显示的字段
                mv.addObject("fieldsHiddenList", fieldsHiddenList);	//隐藏的字段
                mv.addObject("type", params.get("type"));
            }catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/logisticsmmodeGrads_add");
        }
        return mv;
    }

    /**
     * 打开基础数据编辑页
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:edit")
    @GetMapping("/edit")
    public ModelAndView baseInfoEdit(@RequestParam Map<String, Object> params){
        ModelAndView mv = this.getModelAndView();

        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "moduleTable","基础数据表标识");
        }
        if (mv.isEmpty()){
            mv = this.gotoModelAndViewErrorPage(params, "guid","整表唯一标识");
        }
        if (mv.isEmpty()){
            params.put("entity", new LogisticsMmodeGrads());
            AjaxResult ajaxResult = new AjaxResult();
            List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>(); //字段字典
            List<Map<String, Object>> fieldsListTemp = new ArrayList<Map<String, Object>>(); //临时存储区，用于存放需要放到最后生成的控件，如：备注等大文本控件
            List<Object> fieldsList = new ArrayList<Object>(); 			//生成控件的字段和属性
            List<Object> fieldsHiddenList = new ArrayList<Object>(); 	//隐藏的字段和属性
            try{
                params.put("getFieldAttributesType", "pagenotingrid"); 	//非表格字段
                dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
                if (dicFieldsList.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                    LogisticsMmodeGrads logisticsMmodeGrads = logsticsMmodeGradsMapper.queryFirstByGuid(params);
                    if (logisticsMmodeGrads == null){
                        ajaxResult = AjaxResult.error("没有符合条件的数据。");
                    }else{
                        ajaxResult = AjaxResult.success("查询成功。");
                        for (SysDictionaryForPageField dicFields : dicFieldsList){
                            Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);
                            Map<String, Object> baseInfoMap = TypeUtils.entityToMap(logisticsMmodeGrads);
                            fieldsListMap.put("fieldContent", baseInfoMap.get(fieldsListMap.get("fields")));

                            if (fieldsListMap.get("fieldFrontType") == null){
                                fieldsListMap.put("fieldFrontType", "t_text");
                            }

                            if ("t_textarea".equals(fieldsListMap.get("fieldFrontType").toString())){ //大文本控件放入临时存储区
                                fieldsListTemp.add(fieldsListMap);
                            }else if ((Integer)fieldsListMap.get("fieldFrontTop") == 1){	//显示的字段
                                fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
                            }else if ((Integer)fieldsListMap.get("fieldFrontTop") == 2){	//隐藏的字段
                                fieldsHiddenList.add(TypeUtils.mapToJson(fieldsListMap));
                            }
                        }
                        if (!fieldsListTemp.isEmpty() && fieldsListTemp.size() > 0){
                            for (Map<String, Object> fieldsMap : fieldsListTemp){ 			//大文本控件追加到数组末尾
                                if ((Integer)fieldsMap.get("fieldFrontTop") == 1){			//显示的字段
                                    fieldsList.add(TypeUtils.mapToJson(fieldsMap));
                                }else if ((Integer)fieldsMap.get("fieldFrontTop") == 2){	//隐藏的字段
                                    fieldsHiddenList.add(TypeUtils.mapToJson(fieldsMap));
                                }
                            }
                        }
                    }
                    mv.addObject("fieldsList", fieldsList);				//显示的字段
                    mv.addObject("fieldsHiddenList", fieldsHiddenList);	//隐藏的字段
                }
            }catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/logisticsmmodeGrads_add");
        }
        return mv;
    }

    /**
     * 新增基础数据
     * @param params 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:add")
    @Log(title = "基础数据新增", businessType = BusinessType.INSERT)
    @PostMapping("/addpost")
    @ResponseBody
    public AjaxResult baseInfoAddPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(params)) {
            return  AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= logisticsMmodeGradsService.insertLogisticsMmodeGrads(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    /**
     * 删除物流基础信息
     * @author fxz
     * @date: 2022-09-22
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:edit")
    @Log(title = "删除物流基础信息", businessType = BusinessType.DELETE)
    @PostMapping("/deletepost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult deletepost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= logisticsMmodeGradsService.deleteLogisticsMmodeGrads(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 修改基础数据
     * @param params 对应实体类的json格式字符串，示例：[{"key11":"value11","key12":"value12"},{"key21":"value21","key22":"value22"}]
     * @return MessageModel
     * @author DH-Xxj
     */
    @RequiresPermissions("logstics:logisticsmmodegrads:edit")
    @Log(title = "修改基础数据", businessType = BusinessType.INSERT)
    @PostMapping("/editpost")
    @ResponseBody
    public AjaxResult baseInfoEditPost(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(params)) {
            return  AjaxResult.error("数据提交失败，必填参数有误！");
        }
        try {
            ajaxResult= logisticsMmodeGradsService.updateLogisticsMmodeGrads(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
